package pers.xiaojun.boot.module.system.service.tenant;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import pers.xiaojun.boot.common.enums.CommonStatusEnum;
import pers.xiaojun.boot.common.exception.BusinessException;
import pers.xiaojun.boot.module.system.controller.tenant.vo.packages.TenantPackageQueryReqVO;
import pers.xiaojun.boot.module.system.controller.tenant.vo.packages.TenantPackageSaveReqVO;
import pers.xiaojun.boot.module.system.dao.entity.tenant.TenantPackageDO;
import pers.xiaojun.boot.module.system.dao.mapper.tenant.TenantPackageMapper;
import pers.xiaojun.boot.module.system.service.permission.MenuService;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.mybatis.core.query.LambdaQueryWrapperX;

import java.util.Collection;
import java.util.Objects;

import static pers.xiaojun.boot.module.system.constants.BusinessCodeConstants.*;

/**
 * 描述
 *
 * @author xiaojun
 * @since 2025-11-22
 */
@Service
@RequiredArgsConstructor
public class TenantPackageServiceImpl extends ServiceImpl<TenantPackageMapper, TenantPackageDO> implements TenantPackageService {

    @Autowired
    @Lazy
    private MenuService menuService;

    @Override
    public TenantPackageDO saveTenantPackage(TenantPackageSaveReqVO saveVO) {
        // 校验套餐基础信息
        validateTenantPackageBasicInfo(null, saveVO.getName());
        // 校验菜单
        menuService.validateMenusExists(saveVO.getMenuIds());

        TenantPackageDO tenantPackage = BeanUtil.toBean(saveVO, TenantPackageDO.class);
        this.baseMapper.insert(tenantPackage);

        return tenantPackage;
    }

    @Override
    public TenantPackageDO updateTenantPackage(TenantPackageSaveReqVO updateVO) {
        // 校验租户套餐是否存在
        validateTenantPackageExists(updateVO.getId());
        // 校验套餐基础信息
        validateTenantPackageBasicInfo(updateVO.getId(), updateVO.getName());
        // 校验菜单
        menuService.validateMenusExists(updateVO.getMenuIds());

        TenantPackageDO tenantPackage = BeanUtil.toBean(updateVO, TenantPackageDO.class);
        this.baseMapper.updateById(tenantPackage);

        return tenantPackage;
    }

    @Override
    public void removeTenantPackageById(Long tenantPackageId) {
        // 校验租户套餐是否存在
        validateTenantPackageExists(tenantPackageId);
    }

    @Override
    public void removeTenantPackageByIds(Collection<Long> tenantPackageIds) {

    }

    @Override
    public TenantPackageDO getTenantPackageById(Long tenantPackageId) {
        return validateTenantPackageExists(tenantPackageId);
    }

    @Override
    public Page<TenantPackageDO> listTenantPackagesPage(TenantPackageQueryReqVO queryVO) {
        return this.baseMapper.selectPage(PageConvert.INSTANCE.toPage(queryVO), new LambdaQueryWrapperX<TenantPackageDO>()
                .likeIfPresent(TenantPackageDO::getName, queryVO.getName())
                .eqIfPresent(TenantPackageDO::getStatus, queryVO.getStatus())
                .betweenIfPresent(TenantPackageDO::getCreateTime, queryVO.getCreateTime())
        );
    }

    @Override
    public TenantPackageDO validateTenantPackageExists(Long tenantPackageId) {
        TenantPackageDO tenantPackage = this.baseMapper.selectById(tenantPackageId);
        if (tenantPackage == null) {
            throw new BusinessException(TENANT_PACKAGE_NOT_EXISTS);
        }
        return tenantPackage;
    }

    @Override
    public TenantPackageDO validateTenantPackageAvailable(Long tenantPackageId) {
        TenantPackageDO tenantPackage = validateTenantPackageExists(tenantPackageId);
        if(CommonStatusEnum.isDisable(tenantPackage.getStatus())) {
            throw new BusinessException(TENANT_PACKAGE_DISABLED);
        }
        return tenantPackage;
    }

    /**
     * 校验租户套餐基础信息
     *
     * @param tenantPackageId 租户套餐Id
     * @param name            租户套餐名称
     */
    private void validateTenantPackageBasicInfo(Long tenantPackageId, String name) {
        TenantPackageDO tenantPackage;
        if (name != null) {
            tenantPackage = this.baseMapper.selectByName(name);
            if (tenantPackage != null && !Objects.equals(tenantPackage.getId(), tenantPackageId)) {
                throw new BusinessException(TENANT_PACKAGE_NAME_EXISTS);
            }
        }
    }

}
